package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.param.AttributeUpdateInfoParam;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.service.IAttributeService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Validated
@RestController
@RequestMapping("/attributes")
@Api(tags = "05. 属性管理")
public class AttributeController {

    @Autowired
    IAttributeService attributeService;

    // http://localhost:9180/attributes/add-new
    @PostMapping("/add-new")
    @ApiOperation("新增属性")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【新增属性】的请求，参数ID：{}", attributeAddNewParam);
        attributeService.addNewAttribute(attributeAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/attributes/{id}/delete
    @PostMapping("/{id:^\\d+$}/delete")
    @ApiOperation("根据ID删除属性")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "请输入合法的ID值！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据ID删除属性】的请求，参数ID：{}", id);
        attributeService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/attributes/{id}/update
    @PostMapping("/{id:^\\d+$}/update")
    @ApiOperation("根据ID修改属性详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult update(@Range(min = 1, message = "请输入合法的ID值！")
                             @PathVariable Long id, AttributeUpdateInfoParam attributeUpdateInfoParam) {
        log.debug("开始处理【根据ID修改属性详情】的请求，参数ID：{}", id);
        attributeService.updateById(id, attributeUpdateInfoParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/attributes/{id}
    @GetMapping("/{id:^\\d+$}")
    @ApiOperation("根据ID查询属性详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性ID", required = true, dataType = "long")
    })
    public JsonResult getStandard(@Range(min = 1, message = "请输入合法的ID值！")
                           @PathVariable Long id) {
        log.debug("开始处理【根据ID查询属性详情】的请求，参数ID：{}", id);
        AttributeStandardVO standard = attributeService.getStandardById(id);
        return JsonResult.ok(standard);
    }

    // http://localhost:9180/attributes/list/{id}
        @GetMapping("/list/{id:^\\d+$}")
    @ApiOperation("根据属性模板ID查询属性列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "属性模板ID", required = true, dataType = "long")
    })
    public JsonResult list(@Range(min = 1, message = "请输入合法的ID值！")
                           @PathVariable Long id) {
        log.debug("开始处理【根据属性模板ID查询属性列表】的请求，参数ID：{}", id);
        PageData<AttributeListItemVO> pageData = attributeService.listByAttributeTemplateId(id);
        return JsonResult.ok(pageData);
    }
}
